package com.controller;

import com.Service.CommonMenu;
import com.Service.UserService;
import com.ServiceImpl.UserServiceImpl;
import com.pojo.User;

import java.util.Scanner;
public class CommonMenuImpl implements CommonMenu {
    private Scanner sc;
    private UserService commonService;

    public CommonMenuImpl() {
        sc = new Scanner(System.in);
        commonService = new UserServiceImpl();
    }

    @Override
    public void choice() {
        System.out.println("==========欢迎光临租车系统==============");
        System.out.println("请选择您的操作：1、登录  2、注册  3、退出");
        String Choice = sc.next();
        switch (Choice) {
            case "1":
                sign_in();
                break;
            case "2":
                login();
                break;
            case "3":
                break;
           default:
               System.out.println("输入错误");
               choice();
               return;
        }
    }

    @Override
    public void sign_in() {
        System.out.print("用户名：");
        String username=sc.next();
        System.out.print("密码：");
        String password=sc.next();
        User user = commonService.getUserByUsernameAndPassword(username, password);
        if (user==null){
            System.out.println("登录失败");
            choice();
            return;
        }else{
            if (user.isAdmin()){
                new AdminMenuImpl(user).mainMenu();
            }else{
                new UserMenuImpl(user).main_Menu();
            }
        }
    }

    @Override
    public void login() {
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码");
        String password=sc.next();
        System.out.println("请输入再次密码");
        String password_again=sc.next();

        if (password.equals(password_again)){
            if (commonService.login(username, password)){
                System.out.println("注册成功");
            }else{
                System.out.println("注册失败");
            }
            System.out.println("按任意键返回上一层");
            sc.next();
            choice();
            return;
        }else {
            System.out.println("两次密码不一致");
            login();
            return;
        }
    }

}
